package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.pojo.User;
import com.fjh.springboot_01.service.TutoringService;
import com.fjh.springboot_01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页控制器，提供主页所需的数据
 */
@RestController
@RequestMapping("/api/home")
public class HomeController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private TutoringService tutoringService;
    
    /**
     * 获取主页数据，包括热门教师、最新家教信息等
     * @return 主页数据
     */
    @GetMapping
    public Result<Map<String, Object>> getHomeData() {
        Map<String, Object> data = new HashMap<>();
        
        // 获取热门教师（按评分排序）
        List<User> popularTeachers = userService.getPopularTeachers(5);
        data.put("popularTeachers", popularTeachers);
        
        // 获取最新家教信息
        data.put("latestTutoring", tutoringService.getLatestTutoring(5));
        
        // 获取学科分类统计
        data.put("subjectStats", tutoringService.getSubjectStatistics());
        
        return Result.success(data);
    }
    
    /**
     * 获取平台统计数据
     * @return 平台统计数据
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getPlatformStats() {
        Map<String, Object> stats = new HashMap<>();
        
        // 获取平台教师数量
        stats.put("teacherCount", userService.getTeacherCount());
        
        // 获取平台学生数量
        stats.put("studentCount", userService.getStudentCount());
        
        // 获取平台家教信息数量
        stats.put("tutoringCount", tutoringService.getTutoringCount());
        
        // 获取平台已完成的家教数量
        stats.put("completedTutoringCount", tutoringService.getCompletedTutoringCount());
        
        return Result.success(stats);
    }
} 